package com.hexu.event.dao;

import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class LoginDao {

    //从用户表找到手机号对应的密码
    public String findPasswordFromUser(String phoneNumber){
        String sql_1 = "SELECT password FROM user WHERE phone_number = ? ;";
        String password1 = null;
        Connection conn = null;
        PreparedStatement ps1 = null;
        ResultSet rs1 = null;
        try {
            conn = JdbcUtil.getConn();
            ps1 = conn.prepareStatement(sql_1);
            ps1.setString(1, phoneNumber);
            rs1 = ps1.executeQuery();
            //如果手机号有对应密码就返回该密码
            if (rs1.next()) {
                password1 = JdbcUtil.decode(rs1.getString("password"));
            }
            return password1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps1, rs1);
        }
        //没有对应密码就返回null
        return null;
    }

    //从管理员表找到手机号对应的密码
    public String findPasswordFromAdministrator(String phoneNumber){
        String sql_2 = "SELECT password FROM administrator WHERE phone_number = ? ;";
        String password2 = null;
        Connection conn = null;
        PreparedStatement ps2 = null;
        ResultSet rs2 = null;
        try {
            conn = JdbcUtil.getConn();
            ps2 = conn.prepareStatement(sql_2);
            ps2.setString(1, phoneNumber);
            rs2 = ps2.executeQuery();
            //如果手机号有对应密码就返回该密码
            if (rs2.next()) {
                password2 = JdbcUtil.decode(rs2.getString("password"));
            }
            return password2;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps2, rs2);
        }
        //没有对应密码就返回null
        return null;
    }

    //保存管理员密码
    public boolean remember(String phoneNumber){
        String sql = "update administrator set remember=1 where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, phoneNumber);

            ps.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }

    //保存用户密码
    public boolean remember2(String phoneNumber){
        String sql = "update user set remember=1 where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, phoneNumber);

            ps.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }

    //不保存管理员密码
    public boolean notRemember(String phoneNumber){
        String sql = "update administrator set remember=0 where phone_number=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, phoneNumber);

            ps.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }

    //不保存管用户密码
    public boolean notRemember2(String phoneNumber){
        String sql = "update user set remember=0 where phone_number=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, phoneNumber);

            ps.executeUpdate();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }

    //判断某使用者是否保存密码
    public Boolean ifRemember(String phoneNumber){
     String sql = "SELECT remember FROM USER where phone_number=? UNION ALL SELECT remember FROM administrator" +
             " where phone_number=?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,phoneNumber);
            ps.setString(2,phoneNumber);
            rs = ps.executeQuery();
            while (rs.next()){
                int remember = rs.getInt("remember");
                if( remember == 1){
                    return true;
                }
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

}
